import { AIModel, ModelComparison } from '@/types/model';
import axiosInstance from './axiosInstance';
import { envConfig } from '@/config/env';
import { mockModels, modelComparisons } from '@/mocks/data';

// 模拟API延迟
const mockDelay = (ms: number = 500) => new Promise(resolve => setTimeout(resolve, ms));


/**
 * 获取所有可用模型
 */
export const getModels = async (): Promise<{models: AIModel[]}> => {
  if (envConfig.mockEnable) {
    await mockDelay();
    return { models: [...mockModels] };
  }
  
  return axiosInstance.get('/models');
};

/**
 * 获取模型详情
 */
export const getModelDetail = async (id: string): Promise<{model: AIModel}> => {
  if (envConfig.mockEnable) {
    await mockDelay();
    const model = mockModels.find(m => m.id === id);
    if (!model) {
      throw new Error('模型不存在');
    }
    return { model: {...model} };
  }
  
  return axiosInstance.get(`/models/${id}`);
};

/**
 * 获取模型对比数据
 */
export const getModelComparisons = async (days: number = 30): Promise<{comparisons: ModelComparison[]}> => {
  if (envConfig.mockEnable) {
    await mockDelay();
    return { comparisons: [...modelComparisons] };
  }
  return axiosInstance.get('/models/comparisons', { params: { days } });
};

/**
 * 设置默认模型
 */
export const setDefaultModel = async (id: string): Promise<{success: boolean; models: AIModel[]}> => {
  if (envConfig.mockEnable) {
    await mockDelay();
    // 模拟更新默认模型
    const updatedModels = mockModels.map(model => ({
      ...model,
      isDefault: model.id === id
    }));
    
    // 更新模拟数据
    mockModels.splice(0, mockModels.length, ...updatedModels);
    
    return {
      success: true,
      models: [...updatedModels]
    };
  }
  
  return axiosInstance.post(`/models/${id}/default`);
};
    